package com.zzt.gson.util;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

/**
 * @author: zeting
 * @date: 2022/11/16
 */
public class IntTypeAdapter extends TypeAdapter<Integer> {
    @Override
    public void write(JsonWriter out, Integer value) throws IOException {
        if (value == null) {
            out.value(0);
            return;
        }
        out.value(value);
    }

    @Override
    public Integer read(JsonReader in) throws IOException {
        if (in == null) {
            return 0;
        } else if (in.peek() == JsonToken.NULL) {
            in.nextNull();
            return 0;
        }
        return in.nextInt();
    }
}
